from basics.Base import Login
from basics.assert_basics import check_all_key
from basics.parser_files import parse_json


class Goods(Login):

    def shop_goods(self, goods_name=None):
        data = {
            "url": "/adminapi/shop/goods",
            "method": "POST",
            "headers": {'Content-Type': 'application/json', 'token': self.server_token},
            "json": {
                'goods_type': 'real',
                'goods_name': goods_name,
                'sub_title': '好吃的大肉排',
                'goods_image': 'addon/shop/attachment/active_cube_01.png',
                'goods_video': '',
                'goods_category': [
                    2,
                ],
                'brand_id': 1,
                'poster_id': 1,
                'label_ids': [
                    2,
                ],
                'service_ids': [],
                'supplier_id': '',
                'status': '1',
                'sort': '1',
                'addon_shop_supplier': [],
                'spec_type': 'single',
                'price': '99',
                'market_price': '',
                'cost_price': '',
                'stock': '1000',
                'sku_no': '10011',
                'unit': '老面馆',
                'virtual_sale_num': '20',
                'member_discount': '',
                'is_limit': 0,
                'limit_type': 1,
                'max_buy': '',
                'min_buy': '1',
                'attr_format': '[]',
                'attr_id': '',
                'goods_desc': '<p>test<br/></p>',
                'skuCheckAll': False,
                'skuIsIndeterminate': False,
                'skuCheckedCities': [],
                'delivery_type': [
                    'store',
                    'local_delivery',
                    'express',
                ],
                'is_free_shipping': 1,
                'fee_type': 'template',
                'delivery_money': '',
                'delivery_template_id': '',
                'weight': '1',
                'volume': '1',
                'goods_sku_data': {},
                'goods_spec_format': [],
            }
        }
        return self.send_request(self.shop_goods.__name__, data).json()

    def goods_status(self, goods_ids=None, status=None):
        data = {
            "url": "/adminapi/shop/goods/status",
            "method": "PUT",
            "headers": {'Content-Type': 'application/json', 'token': self.server_token},
            "json": {'goods_ids': goods_ids, 'status': status}
        }
        return self.send_request(self.goods_status.__name__, data).json()

    def goods_list(self, goods_name=None, status=None):
        data = {
            "url": "/adminapi/shop/goods",
            "method": "GET",
            "headers": {'Content-Type': 'application/json', 'token': self.server_token},
            "params": {
                'page': '1',
                'limit': '10',
                'goods_name': goods_name,
                'goods_type': '',
                'brand_id': '',
                'label_ids': '',
                'start_sale_num': '',
                'end_sale_num': '',
                'start_price': '',
                'end_price': '',
                'status': status,
                'order': '',
                'sort': '',
            }
        }
        return self.send_request(self.goods_list.__name__, data).json()

    def goods_delete(self, goods_ids=None):
        data = {
            "url": "/adminapi/shop/goods/delete",
            "method": "PUT",
            "headers": {'Content-Type': 'application/json', 'token': self.server_token},
            "json": {'goods_ids': goods_ids}
        }
        return self.send_request(self.goods_delete.__name__, data).json()

    def assert_goods_sql_select(self):
        return True

    def assert_goods_list_check_all_key(self,new_data = None):
        old_data = parse_json(str(self.path.good_list_json_path))
        new_data = self.goods_list()["data"]["data"][0]
        return check_all_key(old_data, new_data)


# if __name__ == '__main__':
#     obj = Goods()
